var pageii = null;
var grid = null;
$(function() {

	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
		}, {
			colkey : "code",
			name : "物料BOM编号",
		}, {
			colkey : "name",
			name : "物料BOM名称",
		}, {
			colkey : "specification",
			name : "物料规格",
		}],
		jsonUrl : rootPath + '/item_resource/bom_findByPage.shtml',
		dymCol:true,
		checkbox : true,
		checkValue: "id",
		records: "list",
		totalPages: "pages",
		totalRecords: "total",
		pagecode: '10',
		pageNow : 'pageNum'
	});
	
	$("#addFun").click("click", function() {
		addAccount();
	});
	$("#editFun").click("click", function() {
		editAccount();
	});
	$("#delFun").click("click", function() {
		delAccount();
	});
	$("#associated").click("click",function () {
		//打开关联物料页面
		associated();
		
	})
	$("#search").click("click", function() {
		var searchParams = $("#searchForm").serializeJson();
		grid.setOptions({
			data : searchParams
		});
	});

	//根据下拉框选取物料

	$("#material").change(function() {

		var sel = $("#material option:selected").val();//获取下拉框数据
		if(sel == 0){
			$("#searchForms").val("0")
			var searchParams = $("#searchForms").serializeJson();
			grid.setOptions({
				data : searchParams
			});
		}else if (sel == 2) {
			$("#searchForms").val("2")
			var searchParams = $("#searchForms").serializeJson();
			grid.setOptions({
				data : searchParams
			});
		}else if(sel == 3){
			$("#searchForms").val("3")
			var searchParams = $("#searchForms").serializeJson();
			grid.setOptions({
				data : searchParams
			});
		}else if(sel == 5){
			$("#searchForms").val("5")
			var searchParams = $("#searchForms").serializeJson();
			grid.setOptions({
				data : searchParams
			});
		}else{
			$("#searchForms").val("1")
			var searchParams = $("#searchForms").serializeJson();
			grid.setOptions({
				data : searchParams
			});
		}
	});



});
function editAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	pageii = layer.open({
		title : "编辑物料",
		type : 2,
		area : [ "600px", "50%" ],
		content : rootPath + '/item_resource/item_editUI.shtml?id=' + cbox
	});
}
function addAccount() {
	pageii = layer.open({
		title : "新增物料",
		type : 2,
		area : [ "600", "50%" ],
		content : rootPath + '/item_resource/item_addUI.shtml'
	});
}

function associated() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择关联项");
		return;
	}
	pageii = layer.open({
		title : "添加关联",
		type : 2,
		area : [ "900px", "100%" ],
		content : rootPath + '/item_resource/addAssociated.shtml?id=' + cbox
	});
}

function delAccount() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/item_resource/item_deleteEntity.shtml';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}

function exportExcel(excelName){
    // var mytable = "";//需要导出的数据
    var fileName = excelName+".xlsx"

    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {//没选则导出所有数据
        var mytable = document.getElementById("mytable");//获取table
        var sheet = XLSX.utils.table_to_sheet(mytable);
    } else {//第二种：导出指定的行

        //获取表头标题，就是表中第一行，序号不存在
        var valueTd= document.getElementById ("mytable").rows[0];

        //复制节点
        var clone_valueTd = valueTd.cloneNode(true);
        // debugger;

        //根据选中行，动态拼接table
        // var tableData = "<table><tr>" + valueTd.innerHTML + "</tr>";
        var table = document.createElement('table');
        table.id = "table1";
        table.appendChild(clone_valueTd);//使用原有节点会对原有dom造成影响

        //获取所有表中所有checkbox
        var inputs = document.getElementById("mytable").getElementsByTagName("input");
        console.log("inputs.length:"+inputs.length);
        for(var i=0;i<inputs.length;i++) {//获取选中行
            if (inputs[i].type == "checkbox") {
                if (inputs[i].checked) {
                    var checkedRow = inputs[i];
                    var tr = checkedRow.parentNode.parentNode;
                    var clone_tr = tr.cloneNode(true);//复制
                    console.log(tr.cells[0].innerText);
                    if(tr.cells[0].innerText != ""){//序号不为空则拼接，为空说明是表头标题
                        // tableData += "<tr>" + tr.innerHTML + "</tr>";
                        table.appendChild(clone_tr)
                    }
                    // console.log("为空是表头，不拼接")
                    // console.log("tr...:"+tr);
                }
            }
        }

        // tableData += "</table>";
        // mytable = document.createElement(tableData);
        var sheet = XLSX.utils.table_to_sheet(table);
    }

    openDownloadDialog(sheet2blob(sheet), fileName);
}